// let baseUrl = "http://wh2mcy.top/api/v1";
let sortpage = 1;
let nextPageNumber = 456;
let thistimepage = 1;
let obj = null;
// let page = 1;
let token1 = localStorage.getItem("token");
// 导航栏跳转部分

// 文章分享
function article() {
    location.replace('http://127.0.0.1:5500/%E9%97%AE%E7%AD%94%E4%B8%93%E5%8C%BA/article.html');
}

//主体内容左部
//问答列表

function infor(sort) {
    sortpage = sort
    sign = false;
    sort = Number(sort);
    obj = {
        "page": 1,
        "count": 7,
        "sort": sort,
    };

    $.ajax({ // 调用ajax传递数据
        url: `http://wh2mcy.top/api/v1/api/question/hot`,
        contentType: 'application/json',
        // contentType: 'application/x-www-form-urlencoded',
        type: "POST",
        data: JSON.stringify(obj),
        dataType: "json",
        success: function(res) {
            console.log(res.data.length)
                //第一
                //第一
            if (res.data.length >= 1) {
                let questionOne = document.getElementById('questionOne');

                let viewOne = document.getElementById('viewOne');
                let likeOne = document.getElementById('likeOne');
                let answer_numOne = document.getElementById('answer_numOne');
                questionOne.innerText = res.data[0].question;

                viewOne.innerText = res.data[0].view;
                likeOne.innerText = res.data[0].like;
                answer_numOne.innerText = res.data[0].answer_num;
                let questionIdOne = document.getElementById('questionIdOne');
                questionIdOne.innerText = res.data[0].id;
            }

            //第二
            if (res.data.length >= 2) {
                let questionTwo = document.getElementById('questionTwo');

                let viewTwo = document.getElementById('viewTwo');
                let likeTwo = document.getElementById('likeTwo');
                let answer_numTwo = document.getElementById('answer_numTwo');
                questionTwo.innerText = res.data[1].question;

                viewTwo.innerText = res.data[1].view;
                likeTwo.innerText = res.data[1].like;
                answer_numTwo.innerText = res.data[1].answer_num;
                let questionIdTwo = document.getElementById('questionIdTwo');
                questionIdTwo.innerText = res.data[1].id;
            }


            //第三
            if (res.data.length >= 3) {
                let questionThree = document.getElementById('questionThree');

                let viewThree = document.getElementById('viewThree');
                let likeThree = document.getElementById('likeThree');
                let answer_numThree = document.getElementById('answer_numThree');
                questionThree.innerText = res.data[2].question;

                viewThree.innerText = res.data[2].view;
                likeThree.innerText = res.data[2].like;
                answer_numThree.innerText = res.data[2].answer_num;
                let questionIdThree = document.getElementById('questionIdThree');
                questionIdThree.innerText = res.data[2].id;
            }

            //第四
            if (res.data.length >= 4) {
                let questionFour = document.getElementById('questionFour');

                let viewFour = document.getElementById('viewFour');
                let likeFour = document.getElementById('likeFour');
                let answer_numFour = document.getElementById('answer_numFour');
                questionFour.innerText = res.data[3].question;

                viewFour.innerText = res.data[3].view;
                likeFour.innerText = res.data[3].like;
                answer_numFour.innerText = res.data[3].answer_num;
                let questionIdFour = document.getElementById('questionIdFour');
                questionIdFour.innerText = res.data[3].id;
            }


            //第五
            if (res.data.length >= 5) {
                let questionFive = document.getElementById('questionFive');

                let viewFive = document.getElementById('viewFive');
                let likeFive = document.getElementById('likeFive');
                let answer_numFive = document.getElementById('answer_numFive');
                questionFive.innerText = res.data[4].question;

                viewFive.innerText = res.data[4].view;
                likeFive.innerText = res.data[4].like;
                answer_numFive.innerText = res.data[4].answer_num;
                let questionIdFive = document.getElementById('questionIdFive');
                questionIdFive.innerText = res.data[4].id;
            }


            //第六
            if (res.data.length >= 6) {
                let questionSix = document.getElementById('questionSix');

                let viewSix = document.getElementById('viewSix');
                let likeSix = document.getElementById('likeSix');
                let answer_numSix = document.getElementById('answer_numSix');
                questionSix.innerText = res.data[5].question;

                viewSix.innerText = res.data[5].view;
                likeSix.innerText = res.data[5].like;
                answer_numSix.innerText = res.data[5].answer_num;
                let questionIdSix = document.getElementById('questionIdSix');
                questionIdSix.innerText = res.data[5].id;
            }


            //第七
            if (res.data.length >= 7) {
                let questionSeven = document.getElementById('questionSeven');

                let viewSeven = document.getElementById('viewSeven');
                let likeSeven = document.getElementById('likeSeven');
                let answer_numSeven = document.getElementById('answer_numSeven');
                questionSeven.innerText = res.data[6].question;

                viewSeven.innerText = res.data[6].view;
                likeSeven.innerText = res.data[6].like;
                answer_numSeven.innerText = res.data[6].answer_num;
                let questionIdSeven = document.getElementById('questionIdSeven');
                questionIdSeven.innerText = res.data[6].id;
            }

        },
        error: function(err) {
            // console.log(err)
        }
    })
}
//通过按钮切换
let page1 = document.getElementById('pageOne').innerText;
let page2 = document.getElementById('pageTwo').innerText;
let page3 = document.getElementById('pageThree').innerText;
let page4 = document.getElementById('pageFour').innerText;
let page5 = document.getElementById('pageFive').innerText;
let page6 = document.getElementById('pageSix').innerText;
let page7 = document.getElementById('pageSeven').innerText;

function infor2(sort, page) {
    thistimepage = page
    if (page % 7 == 0) {
        // console.log(1)
        nextPageNumber = 123
            // console.log(nextPageNumber)
    } else { nextPageNumber = 456 }
    page = Number(page);
    console.log(page)
    sortpage = sort
    sign = false;
    obj = {
        "page": page,
        "count": 7,
        "sort": sort,
    };
    $.ajax({ // 调用ajax传递数据
        url: `http://wh2mcy.top/api/v1/api/question/hot`,
        contentType: 'application/json',
        type: "post",
        data: JSON.stringify(obj),
        dataType: "json",
        success: function(res) {
            console.log(res.data.length)
                //第一
            if (res.data.length >= 1) {
                let questionOne = document.getElementById('questionOne');

                let viewOne = document.getElementById('viewOne');
                let likeOne = document.getElementById('likeOne');
                let answer_numOne = document.getElementById('answer_numOne');
                questionOne.innerText = res.data[0].question;

                viewOne.innerText = res.data[0].view;
                likeOne.innerText = res.data[0].like;
                answer_numOne.innerText = res.data[0].answer_num;
                let questionIdOne = document.getElementById('questionIdOne');
                questionIdOne.innerText = res.data[0].id;
            }

            //第二
            if (res.data.length >= 2) {
                let questionTwo = document.getElementById('questionTwo');

                let viewTwo = document.getElementById('viewTwo');
                let likeTwo = document.getElementById('likeTwo');
                let answer_numTwo = document.getElementById('answer_numTwo');
                questionTwo.innerText = res.data[1].question;

                viewTwo.innerText = res.data[1].view;
                likeTwo.innerText = res.data[1].like;
                answer_numTwo.innerText = res.data[1].answer_num;
                let questionIdTwo = document.getElementById('questionIdTwo');
                questionIdTwo.innerText = res.data[1].id;
            }


            //第三
            if (res.data.length >= 3) {
                let questionThree = document.getElementById('questionThree');

                let viewThree = document.getElementById('viewThree');
                let likeThree = document.getElementById('likeThree');
                let answer_numThree = document.getElementById('answer_numThree');
                questionThree.innerText = res.data[2].question;

                viewThree.innerText = res.data[2].view;
                likeThree.innerText = res.data[2].like;
                answer_numThree.innerText = res.data[2].answer_num;
                let questionIdThree = document.getElementById('questionIdThree');
                questionIdThree.innerText = res.data[2].id;
            }

            //第四
            if (res.data.length >= 4) {
                let questionFour = document.getElementById('questionFour');

                let viewFour = document.getElementById('viewFour');
                let likeFour = document.getElementById('likeFour');
                let answer_numFour = document.getElementById('answer_numFour');
                questionFour.innerText = res.data[3].question;

                viewFour.innerText = res.data[3].view;
                likeFour.innerText = res.data[3].like;
                answer_numFour.innerText = res.data[3].answer_num;
                let questionIdFour = document.getElementById('questionIdFour');
                questionIdFour.innerText = res.data[3].id;
            }


            //第五
            if (res.data.length >= 5) {
                let questionFive = document.getElementById('questionFive');

                let viewFive = document.getElementById('viewFive');
                let likeFive = document.getElementById('likeFive');
                let answer_numFive = document.getElementById('answer_numFive');
                questionFive.innerText = res.data[4].question;

                viewFive.innerText = res.data[4].view;
                likeFive.innerText = res.data[4].like;
                answer_numFive.innerText = res.data[4].answer_num;
                let questionIdFive = document.getElementById('questionIdFive');
                questionIdFive.innerText = res.data[4].id;
            }


            //第六
            if (res.data.length >= 6) {
                let questionSix = document.getElementById('questionSix');

                let viewSix = document.getElementById('viewSix');
                let likeSix = document.getElementById('likeSix');
                let answer_numSix = document.getElementById('answer_numSix');
                questionSix.innerText = res.data[5].question;

                viewSix.innerText = res.data[5].view;
                likeSix.innerText = res.data[5].like;
                answer_numSix.innerText = res.data[5].answer_num;
                let questionIdSix = document.getElementById('questionIdSix');
                questionIdSix.innerText = res.data[5].id;
            }


            //第七
            if (res.data.length >= 7) {
                let questionSeven = document.getElementById('questionSeven');

                let viewSeven = document.getElementById('viewSeven');
                let likeSeven = document.getElementById('likeSeven');
                let answer_numSeven = document.getElementById('answer_numSeven');
                questionSeven.innerText = res.data[6].question;

                viewSeven.innerText = res.data[6].view;
                likeSeven.innerText = res.data[6].like;
                answer_numSeven.innerText = res.data[6].answer_num;
                let questionIdSeven = document.getElementById('questionIdSeven');
                questionIdSeven.innerText = res.data[6].id;
            }
        },
        error: function(err) {
            // console.log(err)
        }
    })
}


//头部选项
$(document).ready(function() {
    $(".btn-choose").click(function() {

        //点击过后 不能重复点击
        $(".btn-choose").css("background", 'rgba(255, 255, 255, 1)');
        $('.btn-choose').css('color', '#000000');
        $('#latest').attr("onclick", "infor(1)");
        $('#hottest').attr("onclick", "infor(0)");
        $('#finish').attr("onclick", "infor(2)");
        $('#unfinish').attr("onclick", "infor(3)");
        $(this).css('background', '#dcedff');
        $(this).css('color', '#20A6FF');
        $(this).attr("onclick", "");

        // $.ajax({
        //     url: `${baseUrl}/all`,
        //     type: 'GET',
        //     success: function(res) {
        //         // console.log(res);
        //     },
        //     // 请求失败时执行
        //     error: function(err) {
        //         console.log(err);
        //     }
        // });
    });
});
//头部搜索
//判断是否可以点击
$(document).ready(function() {
    $(".main-search-img").attr("onclick", "");
    $("#search").bind("input propertychange", function() {
        let search = $("#search").val();

        if (search != '') {
            // $(".btn-release").css("background", "#20A6FF");
            $(".main-search-img").attr("onclick", "search()");
            $('.main-release').css('display', 'none');
            // console.log(search);
        }
        if (search == '') {
            // $(".btn-release").css("background", "rgba(179, 179, 179, 1)");
            $(".main-search-img").attr("onclick", "");
            $('.main-release').css('display', '');
            // console.log(ReleaseQuestion);
        }
    });

});

//搜索ajax

function search() {
    sign = false;
    let search = $('#search').val();
    obj = {
        "question": search,
    };
    $.ajax({ // 调用ajax传递数据
        url: `http://wh2mcy.top/api/v1/api/question/search`,
        contentType: 'application/json',
        type: "get",
        // dataType: "json",
        // JSON.stringify(obj)
        data: obj,
        success: function(res) {
            console.log(res.data.length)
                //第一
            if (res.data.length >= 1) {
                let questionOne = document.getElementById('questionOne');

                let viewOne = document.getElementById('viewOne');
                let likeOne = document.getElementById('likeOne');
                let answer_numOne = document.getElementById('answer_numOne');
                questionOne.innerText = res.data[0].question;

                viewOne.innerText = res.data[0].view;
                likeOne.innerText = res.data[0].like;
                answer_numOne.innerText = res.data[0].answer_num;
                let questionIdOne = document.getElementById('questionIdOne');
                questionIdOne.innerText = res.data[0].id;
            }

            //第二
            if (res.data.length >= 2) {
                let questionTwo = document.getElementById('questionTwo');

                let viewTwo = document.getElementById('viewTwo');
                let likeTwo = document.getElementById('likeTwo');
                let answer_numTwo = document.getElementById('answer_numTwo');
                questionTwo.innerText = res.data[1].question;

                viewTwo.innerText = res.data[1].view;
                likeTwo.innerText = res.data[1].like;
                answer_numTwo.innerText = res.data[1].answer_num;
                let questionIdTwo = document.getElementById('questionIdTwo');
                questionIdTwo.innerText = res.data[1].id;
            }


            //第三
            if (res.data.length >= 3) {
                let questionThree = document.getElementById('questionThree');

                let viewThree = document.getElementById('viewThree');
                let likeThree = document.getElementById('likeThree');
                let answer_numThree = document.getElementById('answer_numThree');
                questionThree.innerText = res.data[2].question;

                viewThree.innerText = res.data[2].view;
                likeThree.innerText = res.data[2].like;
                answer_numThree.innerText = res.data[2].answer_num;
                let questionIdThree = document.getElementById('questionIdThree');
                questionIdThree.innerText = res.data[2].id;
            }

            //第四
            if (res.data.length >= 4) {
                let questionFour = document.getElementById('questionFour');

                let viewFour = document.getElementById('viewFour');
                let likeFour = document.getElementById('likeFour');
                let answer_numFour = document.getElementById('answer_numFour');
                questionFour.innerText = res.data[3].question;

                viewFour.innerText = res.data[3].view;
                likeFour.innerText = res.data[3].like;
                answer_numFour.innerText = res.data[3].answer_num;
                let questionIdFour = document.getElementById('questionIdFour');
                questionIdFour.innerText = res.data[3].id;
            }


            //第五
            if (res.data.length >= 5) {
                let questionFive = document.getElementById('questionFive');

                let viewFive = document.getElementById('viewFive');
                let likeFive = document.getElementById('likeFive');
                let answer_numFive = document.getElementById('answer_numFive');
                questionFive.innerText = res.data[4].question;

                viewFive.innerText = res.data[4].view;
                likeFive.innerText = res.data[4].like;
                answer_numFive.innerText = res.data[4].answer_num;
                let questionIdFive = document.getElementById('questionIdFive');
                questionIdFive.innerText = res.data[4].id;
            }


            //第六
            if (res.data.length >= 6) {
                let questionSix = document.getElementById('questionSix');

                let viewSix = document.getElementById('viewSix');
                let likeSix = document.getElementById('likeSix');
                let answer_numSix = document.getElementById('answer_numSix');
                questionSix.innerText = res.data[5].question;

                viewSix.innerText = res.data[5].view;
                likeSix.innerText = res.data[5].like;
                answer_numSix.innerText = res.data[5].answer_num;
                let questionIdSix = document.getElementById('questionIdSix');
                questionIdSix.innerText = res.data[5].id;
            }


            //第七
            if (res.data.length >= 7) {
                let questionSeven = document.getElementById('questionSeven');

                let viewSeven = document.getElementById('viewSeven');
                let likeSeven = document.getElementById('likeSeven');
                let answer_numSeven = document.getElementById('answer_numSeven');
                questionSeven.innerText = res.data[6].question;

                viewSeven.innerText = res.data[6].view;
                likeSeven.innerText = res.data[6].like;
                answer_numSeven.innerText = res.data[6].answer_num;
                let questionIdSeven = document.getElementById('questionIdSeven');
                questionIdSeven.innerText = res.data[6].id;
            }
        },
        error: function(err) {
            console.log(err)
        }
    })
}

//发布问题
//侦测有内容变色
$(document).ready(function() {
    $(".btn-release").attr("onclick", "");
    $("#ReleaseQuestion").bind("input propertychange", function() {
        let ReleaseQuestion = $("#ReleaseQuestion").val();

        if (ReleaseQuestion != '' && ReleaseQuestion.length >= 7 && ReleaseQuestion.length <= 50) {
            $(".btn-release").css("background", "#20A6FF");
            $(".btn-release").attr("onclick", "add()");
            // console.log(ReleaseQuestion);
        }
        if (ReleaseQuestion == '' || ReleaseQuestion.length < 7 || ReleaseQuestion.length > 50) {
            $(".btn-release").css("background", "rgba(179, 179, 179, 1)");
            $(".btn-release").attr("onclick", "");
            // console.log(ReleaseQuestion);
        }
    });

});
// 发布问题ajax
function add() {
    sign = false;
    obj = {
        "question": `${$("#ReleaseQuestion").val()}`,
    };
    $.ajax({ // 调用ajax传递数据
        url: `http://wh2mcy.top/api/v1/api/question/add`,
        contentType: 'application/json',
        type: "POST",
        data: JSON.stringify(obj),
        dataType: "json",
        beforeSend: function(XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("x-token", token1);
        },
        success: function(res) {

            alert("发布成功"); // 反馈信息
        },
        error: function(err) {
            console.log(err)
        }
    })
}
//翻页
let ceshi = 11321312221;
$(document).ready(function() {
    $(".turn-page-div").click(function() {

        //点击过后 不能重复点击
        $(".turn-page-div").css("background", '#FFFFFF');
        $('.turn-page-div').css('color', '#000000');
        $(this).css('background', '#20A6FF');
        $(this).css('color', 'white');
    });
});

function nextPage() {


    if (nextPageNumber == 456) {
        thistimepage = Number(thistimepage);
        thistimepage = thistimepage + 1;
        infor2(sortpage, thistimepage);
        // console.log(nextPageNumber)
        if (thistimepage % 7 == 2) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnTwo').css('background', '#20A6FF');
            $('#pageBtnTwo').css('color', 'white');
        }
        if (thistimepage % 7 == 3) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnThree').css('background', '#20A6FF');
            $('#pageBtnThree').css('color', 'white');
        }
        if (thistimepage % 7 == 4) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnFour').css('background', '#20A6FF');
            $('#pageBtnFour').css('color', 'white');
        }
        if (thistimepage % 7 == 5) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnFive').css('background', '#20A6FF');
            $('#pageBtnFive').css('color', 'white');
        }
        if (thistimepage % 7 == 6) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnSix').css('background', '#20A6FF');
            $('#pageBtnSix').css('color', 'white');
            nextpagebtn()
        }
        if (thistimepage % 7 == 0) {
            $(".turn-page-div").css("background", '#FFFFFF');
            $('.turn-page-div').css('color', '#000000');
            $('#pageBtnSeven').css('background', '#20A6FF');
            $('#pageBtnSeven').css('color', 'white');
            nextpagebtn()
        }

    }
    if (nextPageNumber == 123) {
        page1 = Number(page1);
        page1 = page1 + 7;
        let newpage1 = document.getElementById('pageOne');
        newpage1.innerText = page1;

        page2 = Number(page2);
        page2 = page2 + 7;
        let newpage2 = document.getElementById('pageTwo');
        newpage2.innerText = page2;

        page3 = Number(page3);
        page3 = page3 + 7;
        let newpage3 = document.getElementById('pageThree');
        newpage3.innerText = page3;

        page4 = Number(page4);
        page4 = page4 + 7;
        let newpage4 = document.getElementById('pageFour');
        newpage4.innerText = page4;

        page5 = Number(page5);
        page5 = page5 + 7;
        let newpage5 = document.getElementById('pageFive');
        newpage5.innerText = page5;

        page6 = Number(page6);
        page6 = page6 + 7;
        let newpage6 = document.getElementById('pageSix');
        newpage6.innerText = page6;

        page7 = Number(page7);
        page7 = page7 + 7;
        let newpage7 = document.getElementById('pageSeven');
        newpage7.innerText = page7;
        console.log(nextPageNumber)
        $(".turn-page-div").css("background", '#FFFFFF');
        $('.turn-page-div').css('color', '#000000');
        $('#fistPageBtn').css('background', '#20A6FF');
        $('#fistPageBtn').css('color', 'white');
        infor2(sortpage, page1);
        nextpagebtn()
        if (page3 >= 10) {
            $(".turn-page-div-padding").css("padding-left", "8px");
        }
        if (page1 >= 10) {
            $(".turn-page-div-padding-two").css("padding-left", "8px");
        }
    }

}
/*获取用户信息*/

$.ajax({
    url: 'http://wh2mcy.top/api/v2/api/user/home',
    contentType: 'application/x-www-form-urlencoded',
    type: 'get',
    // headers: {
    //     "Authorization": localStorage.getItem("token") //此处放置请求到的用户token
    // },
    beforeSend: function(XMLHttpRequest) {
        XMLHttpRequest.setRequestHeader("x-token", token1);
    },
    success: function(res) {
        $('#question_number').html(res.data.question_num);
        $('#answer_number').html(res.data.answer_num);
    },
    error: function() {
        console.log('error');
    },
    complete: function() {
        console.log('complete');
    }
})



function btncolor() {
    $(".turn-page-div").css("background", '#FFFFFF');
    $('.turn-page-div').css('color', '#000000');
    $('#fistPageBtn').css('background', '#20A6FF');
    $('#fistPageBtn').css('color', 'white');
}
//侦测是否翻页
function nextpagebtn() {
    if (page % 7 == 0) {
        // console.log(1)
        nextPageNumber = 123
            // console.log(nextPageNumber)
    } else { nextPageNumber = 456 }
}


$(document).ready(function() {
    infor(1);
    btncolor();
});

//个人信息的点击跳转
function personal() {
    location.replace('http://wh2mcy.top/front-web/html/personal.html');
}
//发布回答的点击跳转
// function hotAnswer() {
//     location.replace('http://wh2mcy.top/front-web/html/hotAnswer.html');
// }
//页面传参
function hotAnswer(id) {
    if (id == 1) {
        let questionId = document.getElementById('questionIdOne');
        questionId = questionId.innerText;
        window.open("http://wh2mcy.top/front-web/html/hotAnswer.html?id=" + questionId, "_blank");
    }
    if (id == 2) {
        let questionId = document.getElementById('questionIdTwo');
        questionId = questionId.innerText;
        window.open("http://wh2mcy.top/front-web/html/hotAnswer.html?id=" + questionId, "_blank");
    }
    if (id == 3) {
        let questionId = document.getElementById('questionIdThree');
        questionId = questionId.innerText;
        window.open("http://wh2mcy.top/front-web/html/hotAnswer.html?id=" + questionId, "_blank");
    }
    if (id == 4) {
        let questionId = document.getElementById('questionIdFour');
        questionId = questionId.innerText;
        window.open("http://wh2mcy.top/front-web/html/hotAnswer.html?id=" + questionId, "_blank");
    }
    if (id == 5) {
        let questionId = document.getElementById('questionIdFive');
        questionId = questionId.innerText;
        window.open("http://wh2mcy.top/front-web/html/hotAnswer.html?id=" + questionId, "_blank");
    }
    if (id == 6) {
        let questionId = document.getElementById('questionIdSix');
        questionId = questionId.innerText;
        window.open("http://wh2mcy.top/front-web/html/hotAnswer.html?id=" + questionId, "_blank");
    }
    if (id == 7) {
        let questionId = document.getElementById('questionIdSeven');
        questionId = questionId.innerText;
        window.open("http://wh2mcy.top/front-web/html/hotAnswer.html?id=" + questionId, "_blank");
    }

}